/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DataKetersediaanMobilPanel.java
 *
 * Created on Nov 19, 2011, 4:19:35 PM
 */
package DataMobil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Piyo
 */
public  class DataKetersediaanMobilPanel extends javax.swing.JPanel {
 List<DataMobil> records=new ArrayList<DataMobil>();
 Intermobil daocar;
 private int itemNama=0;
 private int itemMerk=1;
 private int itemWarna=2;
    /** Creates new form DataKetersediaanMobilPanel */
    public DataKetersediaanMobilPanel() {
        initComponents();
        daocar=new Intermobil();
        this.statusAwal();

        tabelMobil.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
            btnTambahUpdate.setText("Ubah");
            int row=tabelMobil.getSelectedRow();
            if(row!=-1){
            isiTeks();
                }
            }
        });
    }
void isiTabel(){
        Object data[][]=new Object[records.size()][6];
        int x=0;
        for(DataMobil b : records){
            data[x][0]=b.getId_mobil();
            data[x][1]=b.getNama_mobil();
            data[x][2]=b.getMerk_Mobil();
            data[x][3]=b.getWarna();
            data[x][4]=b.getStatus();
            x++;
        }
        String judul[]={"ID","Nama","Merk","Warna","Status"};
        tabelMobil.setModel(new DefaultTableModel(data, judul));
        jScrollPane1.setViewportView(tabelMobil);
    }
void kosongkanTeks(){
        IDTF.setText("");
        NAMATF.setText("");
        WARNATF.setText("");
        HARGATF.setText("");
        MERKTF.setText("");
        NOPOLTF.setText("");
        NOSTNKTF.setText("");
        STATUSTF.setText("");
    }
void bersih(){
    txtCari.setText("");
}
void isiTeks(){
        DataMobil b=records.get(tabelMobil.getSelectedRow());
        IDTF.setText(b.getId_mobil());
        NAMATF.setText(b.getNama_mobil());
        WARNATF.setText(b.getWarna());
        HARGATF.setText(String.valueOf(b.getHarga_sewa()));
        MERKTF.setText(b.getMerk_Mobil());
        NOPOLTF.setText(b.getNo_polisi());
        NOSTNKTF.setText(b.getNo_STNK());
        STATUSTF.setText(b.getStatus());
    }
 void loadmobil(){
        try {
            records = daocar.getAll();
        } catch (SQLException ex) {
            Logger.getLogger(DataKetersediaanMobilPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
  void loadCari(int item,String cari){
        if(item==itemNama){
            try {
                records = daocar.getByNamaMobil(cari);
            } catch (SQLException ex) {
                Logger.getLogger(DataKetersediaanMobilPanel.class.getName()).log(Level.SEVERE, null, ex);
                
            }
        }else if(item==itemMerk){
            try {
                records = daocar.getBymrek(cari);
            } catch (SQLException ex) {
                Logger.getLogger(DataKetersediaanMobilPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else if(item==itemWarna){
            try {
                records = daocar.getBywarna(cari);
            } catch (SQLException ex) {
                Logger.getLogger(DataKetersediaanMobilPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else{
            try {
                records = daocar.getAll();
            } catch (SQLException ex) {
                Logger.getLogger(DataKetersediaanMobilPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        this.isiTabel();
    }
   void statusAwal(){
        this.kosongkanTeks();
        this.loadmobil();
        this.isiTabel();
        btnTambahUpdate.setText("Tambah");
    }
    boolean tidakBolehKosong(){
        if(IDTF.getText().isEmpty() &&
                NAMATF.getText().isEmpty() &&
                WARNATF.getText().isEmpty() &&
                HARGATF.getText().isEmpty() &&
                MERKTF.getText().isEmpty() &&
                NOPOLTF.getText().isEmpty() &&
                NOSTNKTF.getText().isEmpty() &&
                STATUSTF.getText().isEmpty()){
            return false;
        }else{
            return true;
        }
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDesktopPane1 = new javax.swing.JDesktopPane();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        IDtxt = new javax.swing.JLabel();
        warnatxt = new javax.swing.JLabel();
        NamaTxt = new javax.swing.JLabel();
        hargatxt = new javax.swing.JLabel();
        stattxt = new javax.swing.JLabel();
        nopoltxt = new javax.swing.JLabel();
        merktxt = new javax.swing.JLabel();
        nostnktxt = new javax.swing.JLabel();
        HARGATF = new javax.swing.JTextField();
        NAMATF = new javax.swing.JTextField();
        WARNATF = new javax.swing.JTextField();
        IDTF = new javax.swing.JTextField();
        NOPOLTF = new javax.swing.JTextField();
        NOSTNKTF = new javax.swing.JTextField();
        STATUSTF = new javax.swing.JTextField();
        MERKTF = new javax.swing.JTextField();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel16 = new javax.swing.JLabel();
        cmbKategori = new javax.swing.JComboBox();
        jLabel17 = new javax.swing.JLabel();
        txtCari = new javax.swing.JTextField();
        btnCari = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelMobil = new javax.swing.JTable();
        btnTambahUpdate = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();

        jDesktopPane1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setFont(new java.awt.Font("Georgia", 1, 12));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Ketersediaan Mobil");
        jLabel1.setBounds(270, 0, 140, 40);
        jDesktopPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator1.setBounds(0, 40, 700, 10);
        jDesktopPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        IDtxt.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        IDtxt.setForeground(new java.awt.Color(255, 255, 255));
        IDtxt.setText("ID MOBIL");
        IDtxt.setBounds(10, 60, 110, 30);
        jDesktopPane1.add(IDtxt, javax.swing.JLayeredPane.DEFAULT_LAYER);

        warnatxt.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        warnatxt.setForeground(new java.awt.Color(255, 255, 255));
        warnatxt.setText("WARNA MOBIL");
        warnatxt.setBounds(10, 180, 100, 30);
        jDesktopPane1.add(warnatxt, javax.swing.JLayeredPane.DEFAULT_LAYER);

        NamaTxt.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        NamaTxt.setForeground(new java.awt.Color(255, 255, 255));
        NamaTxt.setText("NAMA MOBIL");
        NamaTxt.setBounds(10, 140, 110, 30);
        jDesktopPane1.add(NamaTxt, javax.swing.JLayeredPane.DEFAULT_LAYER);

        hargatxt.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        hargatxt.setForeground(new java.awt.Color(255, 255, 255));
        hargatxt.setText("HARGA SEWA");
        hargatxt.setBounds(10, 220, 110, 30);
        jDesktopPane1.add(hargatxt, javax.swing.JLayeredPane.DEFAULT_LAYER);

        stattxt.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        stattxt.setForeground(new java.awt.Color(255, 255, 255));
        stattxt.setText("STATUS MOBIL");
        stattxt.setBounds(370, 60, 110, 30);
        jDesktopPane1.add(stattxt, javax.swing.JLayeredPane.DEFAULT_LAYER);

        nopoltxt.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        nopoltxt.setForeground(new java.awt.Color(255, 255, 255));
        nopoltxt.setText("NO POLISI");
        nopoltxt.setBounds(10, 100, 100, 30);
        jDesktopPane1.add(nopoltxt, javax.swing.JLayeredPane.DEFAULT_LAYER);

        merktxt.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        merktxt.setForeground(new java.awt.Color(255, 255, 255));
        merktxt.setText("MERK MOBIL");
        merktxt.setBounds(370, 100, 90, 30);
        jDesktopPane1.add(merktxt, javax.swing.JLayeredPane.DEFAULT_LAYER);

        nostnktxt.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        nostnktxt.setForeground(new java.awt.Color(255, 255, 255));
        nostnktxt.setText("NO STNK");
        nostnktxt.setBounds(370, 140, 100, 30);
        jDesktopPane1.add(nostnktxt, javax.swing.JLayeredPane.DEFAULT_LAYER);
        HARGATF.setBounds(120, 220, 220, 30);
        jDesktopPane1.add(HARGATF, javax.swing.JLayeredPane.DEFAULT_LAYER);
        NAMATF.setBounds(120, 140, 220, 30);
        jDesktopPane1.add(NAMATF, javax.swing.JLayeredPane.DEFAULT_LAYER);
        WARNATF.setBounds(120, 180, 220, 30);
        jDesktopPane1.add(WARNATF, javax.swing.JLayeredPane.DEFAULT_LAYER);
        IDTF.setBounds(120, 60, 220, 30);
        jDesktopPane1.add(IDTF, javax.swing.JLayeredPane.DEFAULT_LAYER);
        NOPOLTF.setBounds(120, 100, 220, 30);
        jDesktopPane1.add(NOPOLTF, javax.swing.JLayeredPane.DEFAULT_LAYER);
        NOSTNKTF.setBounds(490, 140, 190, 30);
        jDesktopPane1.add(NOSTNKTF, javax.swing.JLayeredPane.DEFAULT_LAYER);
        STATUSTF.setBounds(490, 60, 190, 30);
        jDesktopPane1.add(STATUSTF, javax.swing.JLayeredPane.DEFAULT_LAYER);
        MERKTF.setBounds(490, 100, 190, 30);
        jDesktopPane1.add(MERKTF, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator2.setBounds(0, 260, 700, 10);
        jDesktopPane1.add(jSeparator2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel16.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        jLabel16.setForeground(new java.awt.Color(255, 255, 255));
        jLabel16.setText("Kategori :");
        jLabel16.setBounds(10, 310, 80, 30);
        jDesktopPane1.add(jLabel16, javax.swing.JLayeredPane.DEFAULT_LAYER);

        cmbKategori.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nama", "Merk", "Warna" }));
        cmbKategori.setBounds(120, 310, 120, 30);
        jDesktopPane1.add(cmbKategori, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel17.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        jLabel17.setForeground(new java.awt.Color(255, 255, 255));
        jLabel17.setText("Item :");
        jLabel17.setBounds(270, 310, 60, 30);
        jDesktopPane1.add(jLabel17, javax.swing.JLayeredPane.DEFAULT_LAYER);
        txtCari.setBounds(330, 310, 180, 30);
        jDesktopPane1.add(txtCari, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnCari.setText("Cari");
        btnCari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCariActionPerformed(evt);
            }
        });
        btnCari.setBounds(530, 310, 160, 30);
        jDesktopPane1.add(btnCari, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator3.setBounds(-20, 300, 720, 20);
        jDesktopPane1.add(jSeparator3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        tabelMobil.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tabelMobil.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jScrollPane1.setViewportView(tabelMobil);

        jScrollPane1.setBounds(0, 360, 700, 100);
        jDesktopPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnTambahUpdate.setText("Tambah");
        btnTambahUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTambahUpdateActionPerformed(evt);
            }
        });
        btnTambahUpdate.setBounds(450, 270, 130, 23);
        jDesktopPane1.add(btnTambahUpdate, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnDelete.setText("Hapus");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        btnDelete.setBounds(70, 270, 110, 23);
        jDesktopPane1.add(btnDelete, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator4.setBounds(0, 350, 700, 10);
        jDesktopPane1.add(jSeparator4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/DataMobil/hucomm5jv7-iam-blue-wallpaper.jpg"))); // NOI18N
        jLabel2.setBounds(0, 0, 700, 490);
        jDesktopPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 703, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 455, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void btnCariActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCariActionPerformed
        // TODO add your handling code here:
if(!txtCari.getText().isEmpty()){
        loadCari(cmbKategori.getSelectedIndex(), txtCari.getText());
bersih();        
}
else{
            statusAwal();
}          
}//GEN-LAST:event_btnCariActionPerformed

private void btnTambahUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTambahUpdateActionPerformed
        // TODO add your handling code here:
       if(btnTambahUpdate.getText().equals("Tambah")){
        if(tidakBolehKosong()){
            try {
                DataMobil b = new DataMobil();
                b.setId_mobil(IDTF.getText());
                b.setNama_mobil(NAMATF.getText());
                b.setWarna(WARNATF.getText());
                b.setHarga_sewa(Integer.parseInt(HARGATF.getText()));
                b.setMerk_Mobil(MERKTF.getText());
                b.setNo_polisi(NOPOLTF.getText());
                b.setNo_STNK( NOSTNKTF.getText());
                b.setStatus(STATUSTF.getText());
                daocar.insert(b);
                
            } catch (SQLException ex) {
                Logger.getLogger(DataKetersediaanMobilPanel.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(this,"error");
            }
        }
    else{
            JOptionPane.showMessageDialog(this, "input tidak boleh ada yang kosong", "Kesalahan", JOptionPane.ERROR_MESSAGE);
        }    
       }else{
            try {
               DataMobil b = new DataMobil();
                b.setId_mobil(IDTF.getText());
                b.setNama_mobil(NAMATF.getText());
                b.setWarna(WARNATF.getText());
                b.setHarga_sewa(Integer.parseInt(HARGATF.getText()));
                b.setMerk_Mobil(MERKTF.getText());
                b.setNo_polisi(NOPOLTF.getText());
                b.setNo_STNK( NOSTNKTF.getText());
                b.setStatus(STATUSTF.getText());
                daocar.update(b);
             } catch (SQLException ex) {
                Logger.getLogger(DataKetersediaanMobilPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        this.statusAwal();
}//GEN-LAST:event_btnTambahUpdateActionPerformed

private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
       try {
            // TODO add your handling code here:
            if(tidakBolehKosong()){


            if(JOptionPane.showConfirmDialog(this, "Anda yakin untuk Menghapus data ?", "Konfirmasi", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
            DataMobil b = records.get(tabelMobil.getSelectedRow());
            daocar.delete(b.getId_mobil());
            this.statusAwal();
            }else{
                this.statusAwal();
            }
            }else{
                JOptionPane.showMessageDialog(this, "Pilih data pada tabel terlebih dahulu..", "Kesalahan", JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DataKetersediaanMobilPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
 this.statusAwal();
}//GEN-LAST:event_btnDeleteActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField HARGATF;
    private javax.swing.JTextField IDTF;
    private javax.swing.JLabel IDtxt;
    private javax.swing.JTextField MERKTF;
    private javax.swing.JTextField NAMATF;
    private javax.swing.JTextField NOPOLTF;
    private javax.swing.JTextField NOSTNKTF;
    private javax.swing.JLabel NamaTxt;
    private javax.swing.JTextField STATUSTF;
    private javax.swing.JTextField WARNATF;
    private javax.swing.JButton btnCari;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnTambahUpdate;
    private javax.swing.JComboBox cmbKategori;
    private javax.swing.JLabel hargatxt;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JLabel merktxt;
    private javax.swing.JLabel nopoltxt;
    private javax.swing.JLabel nostnktxt;
    private javax.swing.JLabel stattxt;
    private javax.swing.JTable tabelMobil;
    private javax.swing.JTextField txtCari;
    private javax.swing.JLabel warnatxt;
    // End of variables declaration//GEN-END:variables
}
